<template>
<div>
      <div style="margin-top: 120px">
        <welTop></welTop>
      </div>
  <div>
    <el-table
      :data="tableData"
      style="width: 100%">
      <el-table-column
        prop="studentId"
        label="学号"
        width="180">
      </el-table-column>
      <el-table-column
        prop="name"
        label="姓名"
        width="200">
      </el-table-column>
      <el-table-column
        prop="count"
        label="票数"
        width="180">
      </el-table-column>
      <el-table-column
        fixed="right"
        label="操作"
        width="100">
        <template slot-scope="scope">
          <el-button type="text" @click="open(scope.row.name,scope.row.studentId,scope.row.count)">投票</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</div>
</template>

<script>
  import welTop from "../../components/welTop";

  export default {
    name: "addVote",
    components: {
      welTop
    },
    data(){
      return{
        tableData:[]
      }
    }, beforeRouteEnter:(to, from, next) => {
      console.log("进入投票页")
      next(vm => {
        vm.getData()
      })
    },beforeRouteLeave:(to, from, next) => {
      console.log('关闭页面')
      next()
    },
    beforeRouteUpdate:(to, from, next) => {
      next(vm=>{
        vm.getData()
      })
    },
    methods:{
      getData: function (){
        var _this=this
        this.$axios.get('http://localhost:8888/student/vote').then(res =>{
          console.log(res)
          if (res.data.code==0){
            if (this.tableData == null){
              this.$message.info("投票还未开始")
            }
            this.tableData = res.data.data
          }else {
            this.$message.error('获取信息失败')
          }
        })
      }, open(name,student_id,count) {
        this.$confirm('确定为  '+name+'  投票吗！！！', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.Vote(student_id,count),
          this.$message({
            type: 'success',
            message: '投票成功!'
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '再想想吧'
          });
        });
      },
      Vote(student_id,count){
        this.$axios.get('http://localhost:8888/student/voting',{
          params:{
            studentId:student_id,
            count:count
          }
        }).then(res => {
          if (res.data.code==0){
            this.$message.info("投票成功")
          }
        }).catch(err => {
          return Promise.reject(err)
        })
      }
    }
  }
</script>

<style scoped>

</style>
